<?php
App::uses('AppModel', 'Model');
App::uses('TextQuestion', 'Model');
App::uses('ScaledQuestion', 'Model');
App::uses('ChoiceQuestion', 'Model');
/**
 * Question Model
 *
 * @property Survey $Survey
 * @property ChoiceQuestion $ChoiceQuestion
 * @property ScaledQuestion $ScaledQuestion
 * @property TextQuestion $TextQuestion
 */
class Question extends AppModel {

	public $questionTypes = array(
		'TextQuestion' => "Text",
		'ChoiceQuestion' => "Multiple Choice",
		'ScaledQuestion' => "Numeric Scale", 
	);

	public function afterFind($results, $primary = false) {
		foreach ($results as &$result) {
			$result['ConcreteQuestion'] = null;

			foreach (array_keys($this->questionTypes) as $questionType) {
				if (array_key_exists($questionType, $result) && $result[$questionType]['id'] != null) {
					$newInstance = new $questionType();
					$newInstance->read(null, $result[$questionType]['id']);
					$result['ConcreteQuestion'] = $newInstance;
				}
				unset($result[$questionType]);
			}
		}

		return $results;
	}	

/**
 * belongsTo associations
 *
 * @var array
 */
	public $belongsTo = array(
		'Survey' => array(
			'className' => 'Survey',
			'foreignKey' => 'survey_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);

/**
 * hasMany associations
 *
 * @var array
 */
	public $hasOne = array(
		'ChoiceQuestion' => array(
			'className' => 'ChoiceQuestion',
			'foreignKey' => 'question_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'ScaledQuestion' => array(
			'className' => 'ScaledQuestion',
			'foreignKey' => 'question_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		),
		'TextQuestion' => array(
			'className' => 'TextQuestion',
			'foreignKey' => 'question_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);

	public $hasMany = array(
		'Response' => array(
			'className' => 'Response',
			'foreignKey' => 'question_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);

}
